<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>新增页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">线路</label>
                        <div class="layui-input-block">
                            <div name="line_id" id="line_id" required value="" ></div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">测试卡数量</label>
                        <div class="layui-input-block">
                            <input type="number" name="test_number" value="1" required lay-verify="required" class="layui-input" min="1">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">测试卡状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="test_switch" lay-filter="test_switch" required lay-skin="switch" />
                            <input type="text" style="display:none" name="test_switch" value="0" required />
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">测试卡2小时状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="test2h_switch" lay-filter="test2h_switch" required lay-skin="switch" />
                            <input type="text" style="display:none" name="test2h_switch" value="0" required />
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">测试卡6小时状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="test6h_switch" lay-filter="test6h_switch" required lay-skin="switch" />
                            <input type="text" style="display:none" name="test6h_switch" value="0" required />
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">测试卡12小时状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="test12h_switch" lay-filter="test12h_switch" required lay-skin="switch" />
                            <input type="text" style="display:none" name="test12h_switch" value="0" required />
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">最大调换数量</label>
                        <div class="layui-input-block">
                            <input type="number" name="max_exchange_number" value="1" required lay-verify="required" class="layui-input" min="1">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">日卡价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="rp" value="0" required lay-verify="required" class="layui-input" min="0.01">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">周卡价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="zp" value="0" required lay-verify="required" class="layui-input" min="0.01">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">月卡价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="yp1" value="0" required lay-verify="required" class="layui-input" min="0.01">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">季卡价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="yp3" value="0" required lay-verify="required" class="layui-input" min="0.01">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">半年卡价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="yp6" value="0" required lay-verify="required" class="layui-input" min="0.01">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">年卡价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="np" value="0" required lay-verify="required" class="layui-input" min="0.01">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">日卡状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="rp_switch" lay-filter="rp_switch" required lay-skin="switch" />
                            <input type="text" style="display:none" name="rp_switch" value="0" required />
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">周卡状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="zp_switch" lay-filter="zp_switch" required lay-skin="switch" />
                            <input type="text" style="display:none" name="zp_switch" value="0" required />
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        
        <script>

            // 相关接口
            const PRIMARY_KEY = "id";
            const INSERT_API = "/app/admin/v1-line-extend/insert";
            
            // 字段 线路ID line_id
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.$.ajax({
                    url: "/app/admin/v1-line/select?format=tree",
                    data: {
                        field: PRIMARY_KEY,
                        order: 'desc'
                    },
                    dataType: "json",
                    success: function (res) {
                        let value = layui.$("#line_id").attr("value");
                        let initValue = value ? value.split(",") : [];
                        layui.xmSelect.render({
                            el: "#line_id",
                            name: "line_id",
                            initValue: initValue,
                            filterable: true,
                            data: res.data, 
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true,
                            layVerify: "required",
                        });
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });
            
            // 字段 测试卡状态:0=关闭,1=开启 test_switch
            layui.use(["form"], function() {
                layui.$("#test_switch").attr("checked", layui.$('input[name="test_switch"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(test_switch)", function(data) {
                    layui.$('input[name="test_switch"]').val(this.checked ? 1 : 0);
                });
            })
            
            // 字段 测试卡2小时状态:0=关闭,1=开启 test2h_switch
            layui.use(["form"], function() {
                layui.$("#test2h_switch").attr("checked", layui.$('input[name="test2h_switch"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(test2h_switch)", function(data) {
                    layui.$('input[name="test2h_switch"]').val(this.checked ? 1 : 0);
                });
            })
            
            // 字段 测试卡6小时状态:0=关闭,1=开启 test6h_switch
            layui.use(["form"], function() {
                layui.$("#test6h_switch").attr("checked", layui.$('input[name="test6h_switch"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(test6h_switch)", function(data) {
                    layui.$('input[name="test6h_switch"]').val(this.checked ? 1 : 0);
                });
            })
            
            // 字段 测试卡12小时状态:0=关闭,1=开启 test12h_switch
            layui.use(["form"], function() {
                layui.$("#test12h_switch").attr("checked", layui.$('input[name="test12h_switch"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(test12h_switch)", function(data) {
                    layui.$('input[name="test12h_switch"]').val(this.checked ? 1 : 0);
                });
            })
            
            // 字段 日卡状态:0=关闭,1=开启 rp_switch
            layui.use(["form"], function() {
                layui.$("#rp_switch").attr("checked", layui.$('input[name="rp_switch"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(rp_switch)", function(data) {
                    layui.$('input[name="rp_switch"]').val(this.checked ? 1 : 0);
                });
            })
            
            // 字段 周卡状态:0=关闭,1=开启 zp_switch
            layui.use(["form"], function() {
                layui.$("#zp_switch").attr("checked", layui.$('input[name="zp_switch"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(zp_switch)", function(data) {
                    layui.$('input[name="zp_switch"]').val(this.checked ? 1 : 0);
                });
            })
            
            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    layui.$.ajax({
                        url: INSERT_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>
</html>
